Scripting
Programmable Blocks allow players to write custom scripts which can interact with any other block in the game. These are written in C#, and can perform any functionality or access any data normally got at through the control panel of a block. This page will form a basic guide on writing scripts and outline how to interact with various blocks. It is not intended as an introduction to C#/programming principles - it may be helpful to look up basic guides elsewhere if you have never written code before. Layout On first opening the code editor you will see 3 methods already in place: * Program() - Serves as a constructor and is only run once per game session. * Save() - Used when the script needs to save its state at the end of a session. * Main() - Run every time the script is executed. This is the only method required for the script to run. Main() will hold most of the functionality for the script. Block Types To interact with a block, it has to be read into a local variable as an object. There are a number of classes used to represent various kinds of blocks, with different public methods and properties. All blocks are subclasses of the IMyTerminalBlock class. For example, a Refinery is represented by the IMyRefinery object which includes specialised functions like IsProducing() and NextItemInQueue(), but also inherits generic functions and properties like 'CustomName' from IMyTerminalBlock. The main block classes worth mentioning are: * IMyTerminalBlock - As already mentioned, forms the base class from which all other block definitions inherit, either directly or through multiple layers of the hierarchy. Mostly includes generic functionality for checking ownership, naming, show on HUD etc. * IMyProductionBlock - This is the class definition for all blocks which produce items (e.g. Refinery, Assembler, Arc Furnace). It defines useful interfaces for these blocks, including: ** IsProducing - Boolean showing whether the block is currently processing something or not. ** IsQueueEmpty - Boolean showing whether items are currently in the queue * IMyTextPanel - Definition for all LCD screens. It includes functionality to format and display text or icons on a screen, such as: ** WritePublicText(string value, bool append=false) - This function sets the text for a screen to the string specified by the value parameter. The optional 'append' parameter can be set to true to append the string to the end of the current content rather than overwriting. Text displayed this way will be visible by anyone on the server - can use WritePrivateText() instead. ** ShowPublicTextOnScreen() - Must be run to actually display the value defined on the screen. GridTerminalSystem All interaction with in-game blocks has to take place through the GridTerminalSystem interface. It provides access to all blocks connected to the same in-game grid as the programmable block being used. In order to start using a block, a local reference to the object must be intialised from the GridTerminalSystem. For example, to intialise an LCD Panel with the name 'LCD Panel 1': IMyTextPanel panel = (IMyTextPanel)GridTerminalSystem.GetBlockWithName("LCD Panel 1"); Note that the value returned by GridTerminalSystem has to be cast into the relevant type (including IMyTerminalBlock). Fetching block by name IMyTerminalBlock block = (IMyTerminalBlock)GridTerminalSystem.GetBlockWithName("Example Name"); Fetching blocks of type List blocks = new List(); GridTerminalSystem.GetBlocksOfType(blocks); * Populates the 'blocks' list with all blocks of the specified type (e.g. Refineries). Fetching blocks by group List blocks = new List(); GridTerminalSystem.GetBlockGroupWithName("Example Group").GetBlocksOfType(blocks); * Populates the 'blocks' list with all blocks which are part of the specified group. Note that 'GetBlockGroupWithName()' returns an object representing the group rather than the individual blocks - using a GetBlocksOfType fetches all blocks from this group. Searching for blocks by name List blocks = new List(); GridTerminalSystem.SearchBlocksOfName("Refinery", blocks); * Populates the 'blocks' list with all blocks which contain the specified string in their name. For example this search would match blocks of name "Refinery", "Station Refinery 1" etc. Block Actions This section is a work in progress For now, refer to https://spaceengineerswiki.com/Programming_Guide/Action_List to find most actions. Using LCD Screens This section is a work in progress Accessing Inventories This section is a work in progress Category:In-game scripts